import { mapState, mapGetters, mapMutations } from 'vuex'

export default {
	computed: {
		...mapState({
			loginedJobsQueue: state => state.app.loginedJobsQueue
		})
	},
	methods: {
		...mapGetters({
			isLogined: 'isLogined'
		}),
		...mapMutations({
			SET_USER_INFO: 'SET_USER_INFO',
			SET_TOKEN: 'SET_TOKEN',
			SET_LOGINED_JOBS_QUEUE: 'SET_LOGINED_JOBS_QUEUE',
			SHIFT_LOGINED_JOBS: 'SHIFT_LOGINED_JOBS'
		}),
		needLoginCheckClickHandler (job) {
			this.SET_LOGINED_JOBS_QUEUE(job)
			if (!this.isLogined()) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
				return false
			}
			
			this.runLoginedJobsQueue()
		},
		runLoginedJobsQueue () {
			this.loginedJobsQueue.forEach((job) => {
				job.funcs && job.funcs.forEach((fn) => {
					setTimeout(() => {
						if (fn.args) {
							fn.body(...fn.args)
						} else {
							fn.body()
						}
					}, fn.delay)
				})
				
				job.methods && job.methods.forEach((method) => {
					setTimeout(() => {
						if (method.args) {
							method.name(...method.args)
						} else {
							method.name()
						}
					}, method.delay)
				})
				
				this.SHIFT_LOGINED_JOBS()
			})
		}
	}
}
